home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue67 / appbars / Unit2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-12-23  |  5.3 KB  |  202 lines

  1. unit Unit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ShellAPI, StdCtrls;
  8.  
  9. const
  10.   wm_AppBarMessage = wm_User;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure FormDestroy(Sender: TObject);
  16.     procedure FormShow(Sender: TObject);
  17.     procedure FormActivate(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.     abd: TAppBarData;
  21.     function GetRequestRect: TRect;
  22.     procedure WMAppBarMessage(var Msg: TMessage); message wm_AppBarMessage;
  23.     procedure WMWindowPosChanged(var Msg: TWMWindowPosMsg); message WM_WindowPosChanged;
  24.     procedure WMMoving(var Msg: TMessage); message WM_Moving;
  25.     procedure WMExitSizeMove(var Msg: TMessage); message WM_ExitSizeMove;
  26.     property RequestRect: TRect read GetRequestRect;
  27.   public
  28.     { Public declarations }
  29.     procedure CreateParams(var Params: TCreateParams); override;
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. var
  38.   F: TextFile;
  39.  
  40. {$R *.DFM}
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. begin
  44.   // fill the AppBarData data structure
  45.   abd.cbSize := sizeof(abd);
  46.   abd.hWnd := Handle;
  47.   abd.uCallBackMessage := wm_AppBarMessage;
  48.   // always start along bottom edge
  49.   abd.uEdge := ABE_BOTTOM;
  50.   abd.rc := RequestRect;
  51.   abd.lParam := 0;
  52.   SHAppBarMessage(ABM_NEW, abd);
  53. end;
  54.  
  55. procedure TForm1.FormDestroy(Sender: TObject);
  56. begin
  57.   SHAppBarMessage(ABM_REMOVE, abd);
  58. end;
  59.  
  60. procedure TForm1.FormShow(Sender: TObject);
  61. begin
  62.   ShowWindow(Application.Handle, SW_HIDE);
  63.   SHAppBarMessage(ABM_QUERYPOS, abd);
  64.   abd.rc.Top := abd.rc.Bottom - Height;
  65.   SHAppBarMessage(ABM_SETPOS, abd);
  66.   SetBounds(abd.rc.Left, abd.rc.Top, abd.rc.Right-abd.rc.Left, abd.rc.Bottom-abd.rc.Top);
  67. end;
  68.  
  69. procedure TForm1.FormActivate(Sender: TObject);
  70. begin
  71.   SHAppBarMessage(ABM_ACTIVATE, abd);
  72. end;
  73.  
  74. procedure TForm1.CreateParams(var Params: TCreateParams);
  75. begin
  76.   inherited CreateParams(Params);
  77.   if Params.ExStyle and WS_EX_TOOLWINDOW = 0 then
  78.     Params.ExStyle := Params.ExStyle + WS_EX_TOOLWINDOW;
  79. end;
  80.  
  81. function TForm1.GetRequestRect: TRect;
  82. begin
  83.   // set the requested Rect
  84.   Result.Left := 0;
  85.   Result.Top := Screen.Height - 50;
  86.   Result.Right := Screen.Width;
  87.   Result.Bottom := Screen.Height;
  88.   case abd.uEdge of
  89.     ABE_TOP: begin
  90.       Result.Left := 0;
  91.       Result.Top := 0;
  92.       Result.Right := Screen.Width;
  93.       Result.Bottom := 50;
  94.     end;
  95.     ABE_LEFT: begin
  96.       Result.Left := 0;
  97.       Result.Top := 0;
  98.       Result.Right := 50;
  99.       Result.Bottom := Screen.Height;
  100.     end;
  101.     ABE_RIGHT: begin
  102.       Result.Left := Screen.Width-50;
  103.       Result.Top := 0;
  104.       Result.Right := Screen.Width;
  105.       Result.Bottom := Screen.Height;
  106.     end;
  107.   end;
  108. end;
  109.  
  110. procedure TForm1.WMAppBarMessage(var Msg: TMessage);
  111. begin
  112.   // hide when fullscreen apps are displayed
  113.   if Msg.wParam = ABN_FULLSCREENAPP then
  114.     if Msg.lParam <> 0 then Hide else Show;
  115.  
  116.   if Msg.wParam = ABN_POSCHANGED then
  117.   begin
  118.     // fill the AppBarData data structure
  119.     abd.rc := RequestRect;
  120.     SHAppBarMessage(ABM_QUERYPOS, abd);
  121.     case abd.uEdge of
  122.       ABE_TOP: abd.rc.Bottom := abd.rc.Top + 50;
  123.       ABE_LEFT: abd.rc.Right := abd.rc.Left + 50;
  124.       ABE_RIGHT: abd.rc.Left := abd.rc.Right - 50;
  125.       ABE_BOTTOM: abd.rc.Top := abd.rc.Bottom - 50;
  126.     end;
  127.     SHAppBarMessage(ABM_SETPOS, abd);
  128.     SetBounds(abd.rc.Left, abd.rc.Top, abd.rc.Right-abd.rc.Left, abd.rc.Bottom-abd.rc.Top);
  129.   end;
  130. end;
  131.  
  132. procedure TForm1.WMWindowPosChanged(var Msg: TWMWindowPosMsg);
  133. begin
  134.   // must send this message to maintain correct Z-order
  135.   SHAppBarMessage(ABM_WINDOWPOSCHANGED, abd);
  136.   inherited;
  137. end;
  138.  
  139. procedure TForm1.WMMoving(var Msg: TMessage);
  140. var
  141.   P: TPoint;
  142. begin
  143.   GetCursorPos(P);
  144.  
  145.   if (P.Y < Screen.Height/Screen.Width*P.X) and
  146.     (P.Y < -(Screen.Height/Screen.Width*P.X)+Screen.Height) then
  147.   begin
  148.     abd.uEdge := ABE_TOP;
  149.     abd.rc := RequestRect;
  150.     SHAppBarMessage(ABM_QUERYPOS, abd);
  151.     abd.rc.Bottom := abd.rc.Top + 50;
  152.     PRect(Msg.lParam)^ := abd.rc;
  153.   end;
  154.  
  155.   if (P.Y >= Screen.Height/Screen.Width*P.X) and
  156.     (P.Y < -(Screen.Height/Screen.Width*P.X)+Screen.Height) then
  157.   begin
  158.     abd.uEdge := ABE_LEFT;
  159.     abd.rc := RequestRect;
  160.     SHAppBarMessage(ABM_QUERYPOS, abd);
  161.     abd.rc.Right := abd.rc.Left + 50;
  162.     PRect(Msg.lParam)^ := abd.rc;
  163.   end;
  164.  
  165.   if (P.Y >= Screen.Height/Screen.Width*P.X) and
  166.     (P.Y >= -(Screen.Height/Screen.Width*P.X)+Screen.Height) then
  167.   begin
  168.     abd.uEdge := ABE_BOTTOM;
  169.     abd.rc := RequestRect;
  170.     SHAppBarMessage(ABM_QUERYPOS, abd);
  171.     abd.rc.Top := abd.rc.Bottom - 50;
  172.     PRect(Msg.lParam)^ := abd.rc;
  173.   end;
  174.  
  175.   if (P.Y < Screen.Height/Screen.Width*P.X) and
  176.     (P.Y >= -(Screen.Height/Screen.Width*P.X)+Screen.Height) then
  177.   begin
  178.     abd.uEdge := ABE_RIGHT;
  179.     abd.rc := RequestRect;
  180.     SHAppBarMessage(ABM_QUERYPOS, abd);
  181.     abd.rc.Left := abd.rc.Right - 50;
  182.     PRect(Msg.lParam)^ := abd.rc;
  183.   end;
  184.  
  185.   inherited;
  186. end;
  187.  
  188. procedure TForm1.WMExitSizeMove(var Msg: TMessage);
  189. begin
  190.   SetBounds(abd.rc.Left, abd.rc.Top, abd.rc.Right-abd.rc.Left, abd.rc.Bottom-abd.rc.Top);
  191.   SHAppBarMessage(ABM_SETPOS, abd);
  192.   inherited;
  193. end;
  194.  
  195.  
  196. initialization
  197.   AssignFile(F, 'c:\temp\debug.log');
  198.   Rewrite(F);
  199. finalization
  200.   CloseFile(F);
  201. end.
  202.